package com.xhsemoticonskeyboard.activity.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xhsemoticonskeyboard.R;
import com.xhsemoticonskeyboard.common.SimpleCommonUtils;
import com.xhsemoticonskeyboard.common.adapter.ChattingListAdapter;
import com.xhsemoticonskeyboard.common.data.ImMsgBean;
import com.xhsemoticonskeyboard.common.utils.ImageLoadUtils;

import java.io.IOException;
import java.util.List;

import sj.keyboard.utils.imageloader.ImageBase;


//
public class SimpleChatBaseQuickAdapter extends BaseMultiItemQuickAdapter<ImMsgBean, BaseViewHolder> {
    Context context;

    public SimpleChatBaseQuickAdapter(@Nullable List<ImMsgBean> data) {
//        super(R.layout.listitem_cha_left_text, data);
        super(data);

        addItemType(ImMsgBean.CHAT_MSGTYPE_TEXT,R.layout.listitem_cha_left_text);
        addItemType(ImMsgBean.CHAT_MSGTYPE_IMG,R.layout.listitem_chat_left_image);
    }

    @Override
    protected void convert(BaseViewHolder helper, ImMsgBean item) {
        if (context==null) {
            context = helper.itemView.getContext();
        }

        switch (helper.getItemViewType()) {
            case ImMsgBean.CHAT_MSGTYPE_TEXT://字符信息
                TextView textView = helper.getView(R.id.tv_content);
                SimpleCommonUtils.spannableEmoticonFilter(textView, item.getContent());
                break;
            case ImMsgBean.CHAT_MSGTYPE_IMG://图片信息
                ImageView iv_image = helper.getView(R.id.iv_image);
                if (ImageBase.Scheme.FILE == ImageBase.Scheme.ofUri(item.getContent())) {
                    String filePath = ImageBase.Scheme.FILE.crop(item.getContent());
                    Glide.with(context)
                            .load(filePath)
                            .into(iv_image);
                } else {
                    try {
                        ImageLoadUtils.getInstance(context).displayImage(item.getContent(), iv_image);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
